package com.hmf.contrller;

import com.hmf.bean.*;
import com.hmf.service.HmfMenuService;
import com.hmf.service.HmfUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody  //将方法的返回值，以特定的格式(json格式的数据)写入到response的body区域，进而将数据返回给客户端。
//上面两个等于@RestController，是上面两个的组合注解
public class HmfUserContrller {

    @RequestMapping("/")
    public String index() {
        return "Hello,My Project";
    }

    @Autowired
    HmfUserService userService;
    @Autowired
    HmfMenuService menuService;

    //注册
    @RequestMapping("/auth/register")
    public ResponseMsg<Map> register(@RequestBody User user) {
        ResponseMsg<Map> res = new ResponseMsg<Map>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        map.put("roleId", user.getRoleId());
        System.out.println(map);
        User u = new User();
        u.setUsername(user.getUsername());
        u.setPassword(user.getPassword());
        u.setRoleId(user.getRoleId());
        System.out.println(u);
        try {
            int users = userService.register(user);
            if (users == 0) {
                res.success("注册成功");
                map.put("state", "200");
            } else {
                map.put("state", "201");
                res.fail("注册失败");
            }
        } catch (Exception e) {
            res.fail("注册失败");
            // 添加日志记录
            e.printStackTrace();
        }
        res.setData(map);
        return res;
    }

    //登录
    @PostMapping("/auth/login")
    public ResponseMsg<Map> login(@RequestBody User user) {
        // 创建ResponseMsg实例来存储响应数据。
        ResponseMsg<Map> res = new ResponseMsg<Map>();
        //创建一个HashMap储存用户名和密码
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        System.out.println(map);

        try {
            //传递map以获取用户列表。
            List<User> users = userService.login(map);
            System.out.println(users);
            if (!users.isEmpty()) {
                //获取用户users列表中的第一个参数，即用户名作为已登录用户。
                User loggedInUser = users.get(0);
                res.success("登陆成功");
                map.put("user", loggedInUser);
                //设置状态为200
                map.put("state", "200");
                //将存储用户信息的map设置为响应的数据部分
                res.setData(map);
            } else {
                map.put("state", "201");
                res.fail("登陆失败");
                res.setData(map);
            }
        } catch (Exception e) {
            res.fail("登陆失败");
        }
        System.out.println(res);
        return res;

    }

    //修改密码
    @RequestMapping(value = "/auth/updatePassword", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseMsg<Integer> updatePassword(@RequestBody User user) {
        ResponseMsg<Integer> res = new ResponseMsg<>();
        try {
            res.success("成功");
            res.setData(userService.updatePassword(user));
        } catch (Exception e) {
            res.fail("失败");
        }
        return res;
    }

    //验证密码是否正确
    @RequestMapping(value = "/auth/verify", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseMsg<Integer> verify(@RequestBody User user) {
        ResponseMsg<Integer> res = new ResponseMsg<>();
        try {
            res.success("成功");
            res.setData(userService.verifyPassword(user));
        } catch (Exception e) {
            res.fail("失败");
        }
        return res;
    }

    // 显示个人信息
    @RequestMapping(value = "/auth/user", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<User> showUser(@RequestBody Map<String, String> request1) {
        ResponseMsg<User> res = new ResponseMsg<>();
        if (request1.containsKey("username")) {
            String username = request1.get("username");
            try {
                User user = userService.queryUserByName(username);
//                System.out.println(user);
                if (user != null) {
                    res.success("查询成功");
                    res.setData(user);
                } else {
                    res.fail("查询失败");
                }

            } catch (Exception e) {
                res.fail("查询失败");
            }
        }
        System.out.println("个人信息："+res);
        return res;
    }

    //所有用户信息
    @RequestMapping(value = "/auth/allUserMsg", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<List<User>> showAllUserMsg() {
        ResponseMsg<List<User>> res = new ResponseMsg<>();
        try {
            List<User> user = userService.userMsg();
            if (user != null) {
                res.success("查询成功");
                res.setData(user);
            } else {
                res.fail("查询失败");
            }

        } catch (Exception e) {
            res.fail("查询失败");
        }
        System.out.println("所有用户信息："+res);
        return res;
    }

    //根据用户姓名查询用户信息
    @RequestMapping(value = "/user/searchUser",method = {RequestMethod.GET, RequestMethod.POST})
    public User searchUser(@RequestBody Map<String, String> requestBody) {
        String username = requestBody.get("username");
        if (username != null) {
            User user = userService.searchUser(username);
            System.out.println("查询信息"+user);
            return user;
        } else {
            // 处理参数错误或缺失的情况
            return null;
        }
    }

    //修改用户信息
    @RequestMapping(value = "/auth/updateMsg",method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<Integer> updateBicycle(@RequestBody User user) {
        ResponseMsg<Integer> res = new ResponseMsg<>();
        try {
            res.success("成功");
            res.setData(userService.updateMsg(user));
        } catch (Exception e) {
            res.fail("失败");
        }
        return res;
    }
}
